/*
    BibleNotebook - A bible reader software
    Copyright (C) 2014  Williams Fernando Lopez

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
 */

package com.hexidec.ekit.action;

import com.hexidec.ekit.EkitCore;
import java.awt.event.ActionEvent;
import javax.swing.JEditorPane;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;


/**
 *
 * @author Williams Lopez
 */
public class SetAnotherFormatAction extends StyledEditorKit.StyledTextAction{

    public static final int STRIKE = 1;
    public static final int SUPER_SCRIPT = 2;
    public static final int SUB_SCRIPT = 3;

    protected EkitCore parentEkit;
    int format;

    public SetAnotherFormatAction(EkitCore ekit, String actionName, int format)
	{
		super(actionName);
		parentEkit = ekit;
                this.format = format;
	}

    public void actionPerformed(ActionEvent e) {
        SimpleAttributeSet atts = new SimpleAttributeSet();
        JEditorPane editor = parentEkit.getTextPane();
        StyledDocument doc = parentEkit.getExtendedHtmlDoc();
        Element el = doc.getCharacterElement(editor.getSelectionStart());
        boolean willEnable = true;
        switch(format){
            case STRIKE:{
                willEnable = StyleConstants.isStrikeThrough(el.getAttributes());
                StyleConstants.setStrikeThrough(atts, !willEnable);
            } break;
            case SUPER_SCRIPT:{
                willEnable = StyleConstants.isSuperscript(el.getAttributes());
                StyleConstants.setSuperscript(atts, !willEnable);
            }   break;
            case SUB_SCRIPT:{
                willEnable = StyleConstants.isSubscript(el.getAttributes());
                StyleConstants.setSubscript(atts, !willEnable);
            }   break;
        }
        setCharacterAttributes(editor, atts, false);
    }

}
